// pages/add/sell.js

var config = require('../../config')
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array1: ['南京大学金陵学院'],
    objectArray1: [
      {
        id: 0,
        name: '南京大学金陵学院'
      }
    ],
    array2: ['传媒学院', '商学院', '外国语学院', '信息科学与工程学院', '城市与土木工程学院', '化学与生命科学学院', '艺术学院'],
    objectArray2: [
      {
        id: 0,
        name: '传媒学院'
      },
      {
        id: 1,
        name: '商学院'
      },
      {
        id: 2,
        name: '外国语学院'
      },
      {
        id: 3,
        name: '信息科学与工程学院'
      },
      {
        id: 4,
        name: '城市与土木工程学院'
      },
      {
        id: 5,
        name: '化学与生命科学学院'
      },
      {
        id: 6,
        name: '艺术学院'
      }
    ],
    array3: ['qq', 'wechat', 'phone'],
    objectArray3: [
      {
        id: 0,
        name: 'qq'
      },
      {
        id: 1,
        name: 'wechat'
      },
      {
        id: 2,
        name: 'phone'
      }
    ],
    index: 0,
    index2: 3,
    schoolId: 0,
    partId: 3,
    contectId:null,
    title: null,
    author: null,
    pubdate: null,
    fprice: null,
    picture: null,
    price: null,
    contect: null,
    userInfo: {},
    logged: false,
    openId: null
  },
  scan: function () {
    var result;
    var _this = this;
    wx.scanCode({
      success: (res) => {
        console.log(res)
        var url = "http://feedback.api.juhe.cn/ISBN";
        wx.request({
          url: url,
          header: {
            'Content-Type': 'text/html;charset=UTF-8'
          },
          data: {
            key: "1dab23f024a8d29a61a34ccb17237ab2",
            sub: res.result
          },

          success: function (e) {
            console.log(e.data.result);
            _this.setData({
              title: e.data.result.title,
              author: e.data.result.author,
              pubdate: e.data.result.pubdate,
              fprice: e.data.result.price,
              picture: e.data.result.images_medium
            });
          }
        })
      }
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      schoolId: e.detail.value
    })
  },
  bindPickerChange2: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index2: e.detail.value,
      partId: e.detail.value
    })
  },
  bindPickerChange3: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var page = this;
    this.setData({
      index3: e.detail.value,
      contectId: page.data.objectArray3[e.detail.value].name
    })
  },

  price: function (e) {
    this.setData({
      price: e.detail.value
    })
  },

  contect: function (e) {
    this.setData({
      contect: e.detail.value
    })
  },


  commit: function (e) {
    wx.showToast({
      title: '提交中',
      icon: 'loading',
      mask: true
    })
    var _this = this;
    wx.request({
      url: `${config.service.host}/weapp/Sinput`,
      data: {
        sellername: _this.data.userInfo.nickName,
        schoolId: _this.data.schoolId,
        partId: _this.data.partId,
        title: _this.data.title,
        author: _this.data.author,
        fprice: _this.data.fprice,
        picture: _this.data.picture,
        price: _this.data.price,
        contect: _this.data.contect,
        contectId: _this.data.contectId,
        openId: _this.data.openId
      },
      method: "GET",
      header: {
        "Content-Type": "application/json"
      },
      success: function (e) {
        if (e.data == "success") {
          wx.showToast({
            title: '提交完成',
            icon: 'success',
            mask: true
          }),
          wx.navigateBack({
            url: 'add'
          })
         
        } else {
          wx.navigateBack({
            url: 'add'
          })
          /*wx.showToast({
            title: '提交失败',
            icon: 'none',
            mask: true
          })*/
          
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '提交失败',
          icon: 'none',
          mask: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("ok");
    console.log(app.globalData.openid);
    this.setData({
      openId: app.globalData.openid,
      userInfo: app.globalData.userInfo,
      logged: app.globalData.logged
    });
    var _this = this;
    wx.request({
      url: `${config.service.host}/weapp/Userinfo`,
      data: {
        openId: _this.data.openId
      },
      method: "GET",
      header: {
        "Content-Type": "application/json"
      },
      success: function (e) {
        console.log(e);
        _this.setData({
          contectId: e.data.contectId,
          contect: e.data.contect
        })
      }
    })
    // var _this = this;
    // wx.getSetting({
    // 	success: function (res) {
    // 		if (res.authSetting['scope.userInfo']) {
    // 			// 已经授权，可以直接调用 getUserInfo 获取头像昵称
    // 			wx.getUserInfo({
    // 				success: function (res) {
    // 					console.log(res.userInfo)
    // 					_this.setData({
    // 						userInfo: res.userInfo,
    // 						logged: true
    // 					})
    // 				}
    // 			})
    // 		}
    // 	}
    // })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  jump:function(){
    wx.navigateTo({
      url: 'add'
    })
  }
})